using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

using SypConst.Garbage;
using SypConst.Path;
using SypEngine.SiteConfig;

namespace SypEngine
{
    public class SypOnlineCommandHandler : SypCommandHandler
    {
        protected override void LoadActions()
        {
            base.LoadActions();
            string[] SiteDirectories = Directory.GetDirectories(SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.TMP_FOLDER, null));
            foreach (string SiteDirectory in SiteDirectories)
            {
                DirectoryInfo dinfo = new DirectoryInfo(SiteDirectory);
                int SiteId = -1;
                if (int.TryParse(dinfo.Name, out SiteId))
                {
                    string pendingActionFile = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.PENDING_ONLINE_FILE, null, int.Parse(dinfo.Name));
                   
                    if (File.Exists(pendingActionFile))
                    {
                        List<SypActionObj> Actions = null;
                        try
                        {
                            XmlSerializer MySerializer = new XmlSerializer(typeof(List<SypActionObj>), new Type[] { typeof(SypActionObj) });
                            FileStream MyStream = new FileStream(pendingActionFile, FileMode.Open);
                            Actions = (List<SypActionObj>)MySerializer.Deserialize(MyStream);
                            MyStream.Close();
                            SypSites.LoadOnlineQueue(SiteId, Actions);
                            File.Delete(pendingActionFile);
                            SypGarbage.free(MySerializer);
                            SypGarbage.free(MyStream);
                            OutputMethod("Onlineall-synchronise", Output.OUT);
                            OutputMethod("Onlineall-synchronise", Output.COMMAND);
                        }
                        catch (Exception)
                        {
                            OutputMethod("PendingOnlineAction : Unexpected error while loading file : " + pendingActionFile, Output.ERROR);
                        }
                    }
                }
            }
            
        }


        protected override void SaveActions()
        {
            base.SaveActions();
            List<SypSyncTable> SiteSyncs = SypSites.getSiteSyncs();
            foreach (SypSyncTable QueueTable in SiteSyncs)
            {
                List<SypActionObj> onlineArray = new List<SypActionObj>(QueueTable.OnlineQueue);
                int SiteId = QueueTable.SiteId;

                string destinationfile = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.PENDING_ONLINE_FILE, null, SiteId);
                if (onlineArray != null && onlineArray.Count > 0)
                {
                    try
                    {
                        XmlSerializer MySerializer = new XmlSerializer(typeof(List<SypActionObj>), new Type[] { typeof(SypActionObj) });
                        StreamWriter MyWriter = new StreamWriter(destinationfile, false, System.Text.Encoding.UTF8);
                        MySerializer.Serialize(MyWriter, onlineArray);
                        MyWriter.Close();
                        SypGarbage.free(MySerializer);
                        SypGarbage.free(MyWriter);
                    }
                    catch (Exception)
                    {
                        OutputMethod("PendingOnlineAction : Unexpected error while processing serialization of : " + destinationfile, Output.ERROR);
                    }
                }
            }
        }



        public SypOnlineCommandHandler(SetText WriteMethod, VisitorCallBack[] Callbacks , SypSiteHandler SypSites)
            : base(WriteMethod, Callbacks, SypSites)
        {
        }

        protected override void PreProcess()
        {
            base.PreProcess();
        }

        protected override void PostProcess()
        {
            base.PostProcess();
        }


    }
}
